package handlers

import (
	"acc-sys/models"
	"acc-sys/pkg"
	"github.com/gin-gonic/gin"
	"path/filepath"
	"strconv"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	email := c.PostForm("email")

	user := models.User{
		Username: username,
		Email:    email,
		Password: pkg.Md5str(password),
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "注册成功",
		})
		return
	}

}

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	var user models.User
	err := models.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不存在",
		})
		return
	}
	if user.Password != pkg.Md5str(password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	token := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
	return

}
func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	userId, _ := strconv.Atoi(c.PostForm("user_id"))

	video := models.Video{
		Title:     title,
		Content:   content,
		Img:       img,
		LikeCount: 0,
		UserID:    userId,
	}
	err := models.DB.Create(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))

	offset := (page - 1) * size

	var count int64
	models.DB.Model(&models.Video{}).Count(&count)

	var video []models.Video
	err := models.DB.Preload("User").Offset(offset).Limit(size).Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg": gin.H{
				"count": count,
				"video": video,
			},
		})
		return
	}
}
func Upload(c *gin.Context) {
	file, err := c.FormFile("img")
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "文件获取失败",
		})
		return
	}

	allow := map[string]bool{
		".png":  true,
		".jpg":  true,
		".jpeg": true,
	}
	ext := filepath.Ext(file.Filename)

	if !allow[ext] {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "图片格式不正确",
		})
		return
	}
	path := "static/img/" + file.Filename
	err = c.SaveUploadedFile(file, path)
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传失败",
		})
		return
	} else {
		url := "http://" + c.Request.Host + "/" + path
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "上传成功",
			"data": url,
		})
		return

	}

}
func Like(c *gin.Context) {
	userId, _ := strconv.Atoi(c.Query("user_id"))
	videoId, _ := strconv.Atoi(c.Query("video_id"))

	like := models.Like{
		UserID:  userId,
		VideoID: videoId,
	}
	err := models.DB.Create(&like).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "点赞记录添加失败",
		})
		return
	}

	var video models.Video
	err = models.DB.Where("id = ?", videoId).First(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "点赞记录不存在",
		})
		return
	}

	video.LikeCount++
	err = models.DB.Save(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "点赞失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "点赞成功",
		})
		return
	}

}
